package com.zuipin.conf;

import java.util.Properties;

import javax.annotation.Resource;

import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.zuipin.quartz.AutomaticAcclaimJob;
import com.zuipin.quartz.CancelOrderJob;
import com.zuipin.quartz.CancelSpecialPriceJob;
import com.zuipin.quartz.CancelTopJob;
import com.zuipin.quartz.ConfimReceiptJob;
import com.zuipin.quartz.ProductTimingShelfJob;
import com.zuipin.quartz.SetSpecialPriceJob;
import com.zuipin.quartz.UpdateStockJob;
import com.zuipin.util.QuartzUtil;

/**
 * 
 */
@Component
@Order(value = 9)
public class QuartzConfig implements CommandLineRunner {
	@Value("${spring.datasource.driver-class-name}")
	private String				driver;
	
	@Value("${spring.datasource.url}")
	private String				url;
	
	@Value("${spring.datasource.username}")
	private String				username;
	
	@Value("${spring.datasource.password}")
	private String				password;
	
	@Resource
	private JobFactory			jobFactory;
	@Resource
	private ApplicationContext	applicationContext;
	
	private Properties quartzProperties() {
		Properties prop = new Properties();
		prop.put("quartz.scheduler.instanceName", "ServerScheduler");
		prop.put("org.quartz.scheduler.instanceId", "AUTO");
		prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
		prop.put("org.quartz.scheduler.jmx.export", "true");
		
		prop.put("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
		prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
		prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
		prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
		prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
		prop.put("org.quartz.jobStore.isClustered", "true");
		prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
		prop.put("org.quartz.threadPool.threadCount", "5");
		
		prop.put("org.quartz.dataSource.quartzDataSource.driver", driver);
		prop.put("org.quartz.dataSource.quartzDataSource.URL", url);
		prop.put("org.quartz.dataSource.quartzDataSource.user", username);
		prop.put("org.quartz.dataSource.quartzDataSource.password", password);
		prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "10");
		return prop;
	}
	
	@Override
	public void run(String... args) throws Exception {
		
		// 定义job
		JobDetailFactoryBean cancelOrderJob = QuartzUtil.createJobDetail(CancelOrderJob.class, "orderGroup", "cancelOrderJob");
		cancelOrderJob.setBeanName("cancelOrderJob");
		cancelOrderJob.afterPropertiesSet();
		
		// 定义job
		JobDetailFactoryBean cancelSpecialPriceJob = QuartzUtil.createJobDetail(CancelSpecialPriceJob.class, "proGroup", "cancelSpecialPriceJob");
		cancelSpecialPriceJob.setBeanName("cancelSpecialPriceJob");
		cancelSpecialPriceJob.afterPropertiesSet();
		
		// 定义job
		JobDetailFactoryBean setSpecialPriceJob = QuartzUtil.createJobDetail(SetSpecialPriceJob.class, "proGroup", "setSpecialPriceJob");
		setSpecialPriceJob.setBeanName("setSpecialPriceJob");
		setSpecialPriceJob.afterPropertiesSet();
		
		// 定义Trigger
		CronTriggerFactoryBean cancelOrderTrigger = QuartzUtil.dialogStatusTrigger(cancelOrderJob.getObject(), "0 */5 * * * ?");
		cancelOrderTrigger.setBeanName("cancelOrderTrigger");
		cancelOrderTrigger.afterPropertiesSet();
		
		// 定义Trigger
		CronTriggerFactoryBean cancelSpecialPriceTrigger = QuartzUtil.dialogStatusTrigger(cancelSpecialPriceJob.getObject(), "0 */5 * * * ?");
		cancelSpecialPriceTrigger.setBeanName("cancelSpecialPriceTrigger");
		cancelSpecialPriceTrigger.afterPropertiesSet();
		
		// 定义Trigger
		CronTriggerFactoryBean setSpecialPriceTrigger = QuartzUtil.dialogStatusTrigger(setSpecialPriceJob.getObject(), "0 0 */1 * * ?");
		setSpecialPriceTrigger.setBeanName("setSpecialPriceTrigger");
		setSpecialPriceTrigger.afterPropertiesSet();
		
		// 定义确认收货job
		JobDetailFactoryBean confimReceiptJob = QuartzUtil.createJobDetail(ConfimReceiptJob.class, "orderGroup", "confimReceiptJob");
		confimReceiptJob.setBeanName("confimReceiptJob");
		confimReceiptJob.afterPropertiesSet();
		
		// 定义确认收货Trigger
		CronTriggerFactoryBean confimReceiptTrigger = QuartzUtil.dialogStatusTrigger(confimReceiptJob.getObject(), "0 0 */2 * * ?");
		confimReceiptTrigger.setBeanName("confimReceiptTrigger");
		confimReceiptTrigger.afterPropertiesSet();
		
		// 定义取消置顶job
		JobDetailFactoryBean cancelTopJob = QuartzUtil.createJobDetail(CancelTopJob.class, "evaluationGroup", "cancelTopJob");
		cancelTopJob.setBeanName("cancelTopJob");
		cancelTopJob.afterPropertiesSet();
		
		// 定义取消置顶Trigger
		CronTriggerFactoryBean cancelTopTrigger = QuartzUtil.dialogStatusTrigger(cancelTopJob.getObject(), "0 */30 * * * ?");
		cancelTopTrigger.setBeanName("cancelTopTrigger");
		cancelTopTrigger.afterPropertiesSet();
		
		// 定义自动好评job
		JobDetailFactoryBean automaticAcclaimJob = QuartzUtil.createJobDetail(AutomaticAcclaimJob.class, "evaluationGroup", "automaticAcclaimJob");
		automaticAcclaimJob.setBeanName("automaticAcclaimJob");
		automaticAcclaimJob.afterPropertiesSet();
		
		// 定义自动好评Trigger
		CronTriggerFactoryBean automaticAcclaimTrigger = QuartzUtil.dialogStatusTrigger(automaticAcclaimJob.getObject(), "0 */30 * * * ?");
		automaticAcclaimTrigger.setBeanName("automaticAcclaimTrigger");
		automaticAcclaimTrigger.afterPropertiesSet();
		
		// 定义更新库存job
		JobDetailFactoryBean updateStockJob = QuartzUtil.createJobDetail(UpdateStockJob.class, "stockGroup", "updateStockJob");
		updateStockJob.setBeanName("updateStockJob");
		updateStockJob.afterPropertiesSet();
		
		// 定义更新库存Trigger
		CronTriggerFactoryBean updateStockTrigger = QuartzUtil.dialogStatusTrigger(updateStockJob.getObject(), "0 */3 * * * ?");
		updateStockTrigger.setBeanName("updateStockTrigger");
		updateStockTrigger.afterPropertiesSet();
		
		// 定义自动上下架job
		JobDetailFactoryBean productTimingShelfJob = QuartzUtil.createJobDetail(ProductTimingShelfJob.class, "proShelfGroup", "productTimingShelfJob");
		productTimingShelfJob.setBeanName("productTimingShelfJob");
		productTimingShelfJob.afterPropertiesSet();
		
		// 定义自动上下架Trigger
		CronTriggerFactoryBean productTimingShelfTrigger = QuartzUtil.dialogStatusTrigger(productTimingShelfJob.getObject(), "0 0 * * * ?");
		productTimingShelfTrigger.setBeanName("productTimingShelfTrigger");
		productTimingShelfTrigger.afterPropertiesSet();
		
		SchedulerFactoryBean factory = new SchedulerFactoryBean();
		factory.setJobFactory(jobFactory);
		factory.setOverwriteExistingJobs(true);
		factory.setStartupDelay(10);
		factory.setQuartzProperties(quartzProperties());
		factory.setAutoStartup(true);
		factory.setApplicationContext(applicationContext);
		factory.setTriggers(cancelOrderTrigger.getObject(), cancelSpecialPriceTrigger.getObject(), setSpecialPriceTrigger.getObject(), confimReceiptTrigger.getObject(),
				cancelTopTrigger.getObject(), automaticAcclaimTrigger.getObject(), updateStockTrigger.getObject(), productTimingShelfTrigger.getObject());// 增加定时器 需要在此处增加
		factory.setBeanName("schedulerFactoryBean");
		factory.afterPropertiesSet();
		factory.start();
	}
}